﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace D3crft.Windows.UserControls {

    public partial class ucButtonAdvanced : UserControl {

        public event EventHandler evClicked;

        Border oBorder;
        Label oLabel;
        Button oButton;

        bool bEnabled = true;
        bool bMouseEntered = false;

        // ####################################################
        // #####    Class Initialization
        // ####################################################
        #region Class Initialization

        public ucButtonAdvanced() {
            do_Initialize();
            do_Preset();
        }

        private void do_Initialize() {
            InitializeComponent();
        }

        private void do_Preset() {
            oBorder = FindName("Border") as Border;
            oLabel = FindName("Label") as Label;
            oButton = FindName("Button") as Button;

            this.Loaded += new RoutedEventHandler(ev_Object_Loaded);
            oButton.MouseEnter += new MouseEventHandler(ev_oButton_MouseEnter);
            oButton.MouseLeave += new MouseEventHandler(ev_oButton_MouseLeave);
            oButton.Click += new RoutedEventHandler(ev_oButton_Click);
            DependencyPropertyDescriptor.FromProperty(Button.IsPressedProperty, typeof(Button)).AddValueChanged(oButton, new EventHandler(ev_oButton_PressedChanged));

            oButton.Width = this.Width;
            oButton.Height = this.Height;
        }

        #endregion

        // ####################################################
        // #####    Public Access
        // ####################################################
        #region Public Access

        public string Text {
            get { return (string)oLabel.Content; }
            set { oLabel.Content = value; }
        }

        public bool Enabled {
            get { return bEnabled; }
            set {
                bEnabled = value;
                if (IsLoaded) {
                    if (bEnabled) {
                        oBorder.Background = Application.Current.FindResource("Gradient_DefaultUC") as Brush;
                        oBorder.BorderBrush = Application.Current.FindResource("Brush_Border") as Brush;
                        oLabel.Foreground = Application.Current.FindResource("Brush_DefaultForeground") as Brush;
                    }
                    else {
                        bMouseEntered = false;
                        oBorder.Background = Application.Current.FindResource("Gradient_DisabledUC") as Brush;
                        oBorder.BorderBrush = Application.Current.FindResource("Brush_DisabledBorder") as Brush;
                        oLabel.Foreground = Application.Current.FindResource("Brush_DisabledForeground") as Brush;
                    }
                }
            }

        }

        #endregion

        // ####################################################
        // #####    Events
        // ####################################################
        #region Events

        private void ev_oButton_MouseEnter(object sender, EventArgs ea) {
            if (!bEnabled) return;
            bMouseEntered = true;
            if (oButton.IsPressed) {
                // press
                oBorder.Background = Application.Current.FindResource("Gradient_PressedUC") as Brush;
                oBorder.BorderBrush = Application.Current.FindResource("Brush_Border") as Brush;
                oLabel.Foreground = Application.Current.FindResource("Brush_PressedForeground") as Brush;
            }
            else {
                // hover
                oBorder.Background = Application.Current.FindResource("Gradient_HighlightedUC") as Brush;
                oBorder.BorderBrush = Application.Current.FindResource("Brush_HighlightedBorder") as Brush;
                oLabel.Foreground = Application.Current.FindResource("Brush_DefaultForeground") as Brush;
            }
        }

        private void ev_oButton_MouseLeave(object sender, EventArgs ea) {
            if (!bEnabled) return;
            bMouseEntered = false;
            if (oButton.IsPressed) {
                // press
                //oBorder.Background = Application.Current.FindResource("Gradient_PressedUC") as Brush;
                //oBorder.BorderBrush = Application.Current.FindResource("Brush_Border") as Brush;
                //oLabel.Foreground = Application.Current.FindResource("Brush_PressedForeground") as Brush;
            }
            else {
                // default
                oBorder.Background = Application.Current.FindResource("Gradient_DefaultUC") as Brush;
                oBorder.BorderBrush = Application.Current.FindResource("Brush_Border") as Brush;
                oLabel.Foreground = Application.Current.FindResource("Brush_DefaultForeground") as Brush;
            }
        }

        private void ev_oButton_Click(object sender, EventArgs ea) {
            if (!bEnabled) return;
            // released
            oBorder.Background = Application.Current.FindResource("Gradient_HighlightedUC") as Brush;
            oBorder.BorderBrush = Application.Current.FindResource("Brush_HighlightedBorder") as Brush;
            oLabel.Foreground = Application.Current.FindResource("Brush_DefaultForeground") as Brush;
            if (evClicked != null) evClicked(this, null);
            //MessageBox.Show("test");
        }

        private void ev_oButton_PressedChanged(object sender, EventArgs ea) {
            if (!bEnabled) return;
            if (oButton.IsPressed) {
                // pressed
                oBorder.Background = Application.Current.FindResource("Gradient_PressedUC") as Brush;
                oBorder.BorderBrush = Application.Current.FindResource("Brush_Border") as Brush;
                oLabel.Foreground = Application.Current.FindResource("Brush_PressedForeground") as Brush;
            }
            else {

                if (bMouseEntered) {
                    // released
                    oBorder.Background = Application.Current.FindResource("Gradient_HighlightedUC") as Brush;
                    oBorder.BorderBrush = Application.Current.FindResource("Brush_HighlightedBorder") as Brush;
                    oLabel.Foreground = Application.Current.FindResource("Brush_DefaultForeground") as Brush;
                }
                else {
                    // default
                    oBorder.Background = Application.Current.FindResource("Gradient_DefaultUC") as Brush;
                    oBorder.BorderBrush = Application.Current.FindResource("Brush_Border") as Brush;
                    oLabel.Foreground = Application.Current.FindResource("Brush_DefaultForeground") as Brush;
                }

            }
        }

        private void ev_Object_Loaded(object sender, EventArgs ea) {
            if (!bEnabled) {
                bMouseEntered = false;
                oBorder.Background = Application.Current.FindResource("Gradient_DisabledUC") as Brush;
                oBorder.BorderBrush = Application.Current.FindResource("Brush_DisabledBorder") as Brush;
                oLabel.Foreground = Application.Current.FindResource("Brush_DisabledForeground") as Brush;
            }
        }

        #endregion
    }
}
